// pages/mytask/mytask.js
import { getCurrentDate } from "../../utils/util"
import { storeApi, commonApi } from '../../api/index';
import { toast } from "../../utils/modal"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: true,
    userInfo: {},
    currentDate: "",
    showDatePopup: false,
    status: 1,
    pageParam: {
      page: 1,
      page_size: 10,
    },
    taskList: [],
    statusMap: {
      '1':"待开始",
      '2':"已取消",
      '3':"待完成",
      '4':"待支付",
      '5':"已支付"
    },
    checkedTab: 1,
    orderStatus: [
      {
        value: 1,
        label: "待开始"
      },
      {
        value: 3,
        label: "待完成"
      },
      {
        value: 4,
        label: "待支付"
      },
      {
        value: 5,
        label: "已支付"
      },
      {
        value: 2,
        label: "已取消"
      },
    ],
    showPopup: false,
    popupType: 1,  // 1: 取消 2: 开始染发 3: 标记完成
    currentOrderId: null,
    // 标记完成用
    selectTypeId: null,
    selectColorId: null,
    selectAreaId: null,
    ran_store_id: null,
    start_at: null,
    minDate: new Date(2025, 0, 1).getTime(),
    total: 0,
  },
  onDisplay() {
    this.setData({ showDatePopup: true });
  },
  onCloseDatePopup() {
    this.setData({ showDatePopup: false });
  },
  formatDate(date) {
    date = new Date(date);
    const currentYear = date.getFullYear()
    const currentMonth =(date.getMonth() + 1)  < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1)
    const currentDay=  date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
    return currentYear + '-' + currentMonth + '-' +currentDay
  },
  onConfirmDatePopup(event) {
    this.setData({
      showDatePopup: false,
      currentDate: this.formatDate(event.detail),
      taskList: [],
       pageParam: {
        page: 1,
        page_size: 10,
      }
    } , ()=>{
      this.getTaskData()
    })
  },

  async getUserInfo() {
    try {
      let res = await commonApi.getUserInfo()
      this.setData({
        userInfo: res,
      })
    } catch (e) { }
  },
  // bindDateChange(e) {
  //   console.log('picker发送选择改变，携带值为', e.detail.value)
  //   this.setData({
  //     currentDate: e.detail.value,
  //     taskList: [],
  //      pageParam: {
  //       page: 1,
  //       page_size: 10,
  //     }
  //   } , ()=>{
  //     this.getTaskData()
  //   })
  // },
  changeTab(e) {
    if (this.data.checkedTab == e.detail.name) {
        return;
    }
    this.setData({
      checkedTab: e.detail.name,
      taskList: [],
      status:e.detail.name,
      pageParam: {
        page: 1,
        page_size: 10,
      }
    },()=>{
        this.getTaskData()
    })
  },
  async getTaskData(){
    try {
      // console.log(this.data.userInfo)
      let res = res = await storeApi.queryRanstoreReservationOrderList({
        ...this.data.pageParam,
        ran_store_employee_id: this.data.userInfo?.employee_info?.id ?? "",
        ran_store_id: this.data.userInfo?.employee_info?.ran_store_id ?? "",
        start_time: `${this.data.currentDate} 00:00:00`,
        end_time: `${this.data.currentDate} 23:59:59`,
        status: this.data.status,
      })
      this.setData({
        taskList: res.list || [],
        total: res.total || 0,
      })
    } catch (e) { 
      console.log(e)
    }
  },
  updateOrder(e) {
    this.onClose()
    var status = e.currentTarget.dataset.status;
    storeApi.updateReservationOrder({
      id : this.data.currentOrderId,
      status: Number(status)
    }).then(resp => {
        this.getTaskData();
    });
  },
  // 代客预约
  toBookForCustomer() {
    wx.navigateTo({
      url: `/pages/book-for-customer/book-for-customer`,
    });
  },
  // 调整项目
  clickAdjusting(e) {
     var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/book-for-customer/book-for-customer?id=${id}`,
    });
  },
  clickPopup(e){
    var num = e.currentTarget.dataset.num;
    var id = e.currentTarget.dataset.id;
     this.setData({
        showPopup: true,
        popupType: num,
        currentOrderId: id
      })
  },
  onClose() {
    this.setData({
      showPopup: false,
    })
  },
  completeOrder(e){
    const target = e.currentTarget.dataset;
    const item = e.currentTarget.dataset.item;
    // console.log(target)
     this.setData({
        // selectTypeId: target.type_id,
        // selectColorId: target.color_id,
        // selectAreaId: target.area_id,
        // currentOrderId: target.id,
        // ran_store_id: target.ran_store_id,
        // start_at: target.start_at,
        selectTypeId: item.hairdye_type_info.id,
        typeName: item.hairdye_type_info.name,
        selectColorId: item.hairdye_color_info.id,
        colorName: item.hairdye_color_info.name,
        selectAreaId: item.hair_region_info.id,
        areaName: item.hair_region_info.name,
        currentOrderId: item.id,
        ran_store_id: item.ran_store_id,
        start_at: item.start_at,
        popupType: target.num,
        showPopup: true,
      })
  },
  async sureComplete(e){
    const { start_at, finish_at } = e.detail
    const { selectTypeId, typeName, selectColorId, colorName , selectAreaId, areaName} = this.data
    if(typeName.includes('到店选')){
      toast('请选择染膏类型')
      return
    }
    if(colorName.includes('到店选')){
      toast('请选择染膏颜色')
      return
    }
    if(areaName.includes('到店选')){
      toast('请选择染发区域')
      return
    }
    await storeApi.updateReservationOrder({
      id : this.data.currentOrderId,
      ran_store_hairdye_type_id : selectTypeId,
      ran_store_hairdye_color_id : selectColorId,
      ran_store_hair_region_id : selectAreaId,
      status: 4,
      start_at,
      finish_at,
    });
    this.onClose()
    this.getTaskData();
  },
  onSelectType(e) {
    console.log('selectTypeId', e.detail.id)
    this.setData({
      selectTypeId: e.detail.id,
      typeName: e.detail.name
    });
  },
  onSelectColor(e) {
    console.log('selectColorId', e.detail.id)
    this.setData({
      selectColorId: e.detail.id,
      colorName: e.detail.name
    });
  },
  onSelectArea(e) {
    console.log('selectAreaId',e.detail.id)
    this.setData({
      selectAreaId: e.detail.id,
      areaName: e.detail.name
    });
  },
  goCall(e) {
    const phoneNumber = e.currentTarget.dataset.phone
    wx.makePhoneCall({
        phoneNumber
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // this.getUserInfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const currentDate = getCurrentDate()
    console.log(currentDate)
      this.setData({
        currentDate
      }, async ()=>{
        await this.getUserInfo();
        this.getTaskData();
      })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    const {total, taskList, pageParam} = this.data
    if(total > taskList.length){
      this.setData({
        pageParam: {
          ...pageParam,
          page: pageParam.page + 1,
        }
      }, async ()=>{
        this.getTaskData();
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: '频繁染白发，就找染博士',
      imageUrl:
        'https://ranboss.oss-cn-beijing.aliyuncs.com/ranstore/share.png',
    };
  }
})